package com.boe.hbd.server.processor;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSON;
import com.boe.hbd.server.cache.PicSeqCache;
import com.boe.hbd.server.dao.PicDao;
import com.boe.hbd.server.model.Pic;
import com.boe.hbd.server.util.FileUtil;

public class PicProcessor extends BaseProcessor {

	private static PicProcessor instance = new PicProcessor();
	private PicDao picDao = PicDao.getInstance();
	private PicSeqCache seq = PicSeqCache.getInstance();

	private PicProcessor() {
	}

	public static PicProcessor getInstance() {
		return instance;
	}

	public String deal(String[] args, HttpServletRequest req) {
		String method = args[2];
		if ("add".equals(method)) {
			add(args, req);

		}
		if ("get_large".equals(method)) {

		}
		if ("get_small".equals(method)) {

		}
		if ("info".equals(method)) {
			// who care?
		}
		if ("view_list".equals(method)) {
			return viewList(args);
		}
		if ("view_first".equals(method)) {

		}
		if ("my".equals(method)) {
			return myPics(args);
		}

		return null;
	}

	private String viewList(String[] args) {

		List<Pic> picList = picDao.findPicsByViewId(Integer.valueOf(args[3]));
		return JSON.toJSONString(picList);
	}

	private String myPics(String[] args) {

		List<Pic> picList = picDao.findPicsByUserId(Integer.valueOf(args[3]));
		return JSON.toJSONString(picList);
	}

	private String add(String[] args, HttpServletRequest req) {
		try {
			int sequence = seq.getPicSeq();
			FileUtil.save(sequence, req.getInputStream());

			Pic pic = new Pic();
			pic.setViewID(Integer.valueOf(args[3]));
			pic.setUserID(Integer.valueOf(args[4]));
			pic.setDis(args[5]);
			pic.setLat(Double.valueOf(args[6]));
			pic.setLon(Double.valueOf(args[7]));
			pic.setSeq(sequence);
			picDao.insert(pic);
			return "SUCCESS_"+sequence;
		} catch (Exception e) {
			return FAILED;
		}

	}

}
